﻿using System;
namespace DO.DreamOnTrader.Quote
{

	/// <summary>
	/// 回補報價的載入類別
	/// </summary>
	public abstract class RecoverTickLoader : TickLoader
	{
		DateTime quoteDay = DateTime.Today;
		string quoteMonth = "";

		private void axXQRecover_RecoverMsg2(object sender, AxXQRECOVERLib._DXQRecoverEvents_RecoverMsg2Event e)
		{
			int rtn;

			rtn = axXQuote.SetData3(e.varChannel, e.varData);
			if (rtn == (int)QuoteType.期貨分時 || rtn == (int)QuoteType.股票分時 || rtn == (int)QuoteType.選擇權分時)
			{
				string quoteMonthPlus = "";
				uint totalqty = 0;
				for (int i = 0; i < axXQuote.GetTblCount(); i++)
				{
					#region 處理axXQuote

					float price = axXQuote.GetTblValue(i, 2) / 1000;
					uint qty = (uint)axXQuote.GetTblValue(i, 5) - totalqty;
					totalqty = (uint)axXQuote.GetTblValue(i, 5);
					string time = axXQuote.GetTblValue(i, 1).ToString().PadLeft(6, '0');
					DateTime TradeTime = quoteDay.AddHours(Convert.ToInt32(time.Substring(0, 2))).AddMinutes(Convert.ToInt32(time.Substring(2, 2))).AddSeconds(Convert.ToInt32(time.Substring(4, 2)));
					string CloseMonth = axXQuote.GetStrValue(0).Substring(axXQuote.GetStrValue(0).Length - 6);

					#endregion

					Tick tick = new Tick(price, filterProduct, qty, TradeTime, CloseMonth, totalqty);

					if (checkCloseMonth(tick))
					{
						//報價的交易時間仍沒換月
						proecessTick(tick);
					}
					else if ( tick.CloseMonth == quoteMonth ) 
					{
						//報價的交易時間己達user設定的換月時間
						quoteMonthPlus = getQuoteMonth(tick.TradeTime);
						break;
					}
				}

				//註冊換月月份
				if (quoteMonthPlus != "")
				{
					axXQRecover.DoRecoverTbl(XQRecover_ComId, filterProduct.QuoteName + quoteMonthPlus);
				}
				else
				{
					toEndQuote();
				}
			}
		}


		AxXQRECOVERLib.AxXQRecover axXQRecover;// = new AxXQRECOVERLib.AxXQRecover();
		AxXQUOTELib.AxXQuote axXQuote;// = new AxXQUOTELib.AxXQuote();

		#region ===元件參數

		const string XQRecover_IP = "202.39.34.43";
		const int XQRecover_Port = 80;
		const int XQRecover_UsingMsg = 2;
		const string XQRecover_ComId = "FUT";

		#endregion

		public RecoverTickLoader(Product _product, int _addQuoteMonth = 0, int _filterMinute = 30)
		{
			filterProduct = _product;
			addQuoteMonth = _addQuoteMonth;
			filterMinute = _filterMinute;

			this.axXQRecover = new AxXQRECOVERLib.AxXQRecover();
			this.axXQuote = new AxXQUOTELib.AxXQuote();

			//----------------控制項必須---------------------//
			#region 此控制項必須的程式碼
			((System.ComponentModel.ISupportInitialize)(this.axXQRecover)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.axXQuote)).BeginInit();

			Controls.Add(this.axXQRecover);
			Controls.Add(this.axXQuote);

			((System.ComponentModel.ISupportInitialize)(this.axXQRecover)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.axXQuote)).EndInit();
			#endregion
			//----------------------------------------------//

			axXQRecover.RecoverMsg2 += new AxXQRECOVERLib._DXQRecoverEvents_RecoverMsg2EventHandler(this.axXQRecover_RecoverMsg2);

			axXQRecover.RecoverIP = XQRecover_IP;
			axXQRecover.RecoverPort = XQRecover_Port;
			axXQRecover.usingMsg = XQRecover_UsingMsg;
		}

		public override bool Start()
		{
			if (DateTime.Now.Hour < 8)
			{
				quoteMonth = getQuoteMonth(quoteDay.AddDays(-1));
			}
			else
			{
				quoteMonth = getQuoteMonth(quoteDay);
			}
			axXQRecover.DoRecoverTbl(XQRecover_ComId, filterProduct.QuoteName + quoteMonth);
			return true;
		}

		#region TickLoader 成員

		public override bool Stop()
		{
			throw new NotImplementedException();
		}

		#endregion

	}
}
